//
// NLSESolverView.m
// NLSE Solver, Created by jmpp on 9/2/10.
// $Id: NLSESolverView.m 16 2010-11-03 14:19:32Z jmpalacios $

/**
* =================================================================
* Copyright (C) 2010, Juan Manuel Palacios. All Rights Reserved.
*
* This software project, which includes this module, is licensed
* as described in the file LICENSE, which you should have received
* as part of this distribution, and may be used only according to
* the terms described therein.
*
* =================================================================
**/


#import "NLSESolverView.h"


@implementation NLSESolverView

@synthesize viewController;
@synthesize openGLContext;


- (id) initWithFrame:(NSRect)frameRect
{
    
    if ( (self = [super initWithFrame:frameRect]) != nil )
    {
        
        appController = [NLSESolverAppController sharedInstance];
        if ( (openGLContext = [[NSOpenGLContext alloc] initWithFormat:[appController sharedPixelFormat]
                                                                shareContext:[appController sharedOpenGLContext]]) == nil )
        {
            [appController applicationShouldQuit:kOpenGLContextUnavailable];
            return nil;
        }
        [(NSNotificationCenter *)[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(surfaceNeedsUpdate:)
                                                     name:NSViewGlobalFrameDidChangeNotification
                                                   object:self];
    }
    return self;
    
}


- (id) init
{
    
    return [self initWithFrame:[self bounds]];

}


- (void) surfaceNeedsUpdate:(NSNotification *) __attribute__((unused)) theNotification
{
    
    [[self openGLContext] update];
    return;
    
}


/* Here the view is set as current for drawing commands, and the OpenGL context made current as well as necessary */
- (void) lockFocus
{
    
    [super lockFocus];

    NSOpenGLContext *context = [self openGLContext];
    if ([context view] != self)
    {
        [context setView:self];
    }
    [context makeCurrentContext];
    
    return;
    
}


/* This is where OpenGL drawing initialization code goes, called automatically when the view's OpenGLContext is made current */
- (void) prepareOpenGL
{
    
    // maybe I could conditionalize these calls to the view being ready to draw the graph, i.e. the libssfm struct being non-nul.
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0,1.0,-1.0,1.0,-1.0,100.0);
    
    return;
    
}


// Needs implementation! (and customization to each sub-view).
- (void) drawEmptyGraph
{
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glClearColor(1.0f,1.0f,1.0f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    return;
    
}


- (void) mouseDown:(NSEvent *)theEvent
{

    NSPoint location = [self convertPoint:[theEvent locationInWindow]
                                 fromView:nil];
    NSLog(@"Mouse Down event at location (%f,%f) detected!",location.x,location.y);
    
    return;

}


- (BOOL) acceptsFirstResponder
{

    return YES;

}


@end
